package com.project.erp.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.project.erp.common.utils.upload.StateProgress;

/**
 * 文件上传公共类
 * 
 * @author zyj
 * 
 */
@Controller
@Scope("prototype")
public class FileUploadAction extends BaseAction {

	private static final long serialVersionUID = 4179126633490477646L;

	// 上传的文件
	private File file;
	// 上传文件的类型
	private String fileContentType;
	// 上传文件的文件名
	private String fileFileName;
	// 多文件上传
	private File[] files;
	// 多文件上传类型
	private String[] filesFileName;
	// 多文件的文件名
	private String[] filesContentType;
	
	// 进度条对象
	private StateProgress stateProgress;

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public File[] getFiles() {
		return files;
	}

	public void setFiles(File[] files) {
		this.files = files;
	}

	public String[] getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(String[] filesFileName) {
		this.filesFileName = filesFileName;
	}

	public String[] getFilesContentType() {
		return filesContentType;
	}

	public void setFilesContentType(String[] filesContentType) {
		this.filesContentType = filesContentType;
	}
	
	public StateProgress getStateProgress() {
		return stateProgress;
	}

	public void setStateProgress(StateProgress stateProgress) {
		this.stateProgress = stateProgress;
	}
	
	/**
	 * 上传文件处理
	 */
	public String fileUpload() throws Exception {
		try {
			if(null != file){
				System.out.println("file:" + file);
				InputStream is = new FileInputStream(file);
				String root = ServletActionContext.getServletContext().getRealPath("/upload");
				System.out.println("root:" + root);

				System.out.println("name:" + this.fileFileName);
				System.out.println("type:" + this.fileContentType);
				File destFile = new File(root, this.fileFileName);
				
				OutputStream os = new FileOutputStream(destFile);
				byte[] b = new byte[1024 * 1024 * 10];
				int length = 0;
				while (true) {
					length = is.read(b);
					if (length < 0)
						break;
					os.write(b, 0, length);
				}
				
				is.close();
				os.close();
				this.getResultMap().put("result", SUCCESS);
			}else{
				System.out.println("files:" + files);
				for (int i = 0; i < files.length; i++) {
					System.out.println("file:" + files[i]);
					InputStream is = new FileInputStream(files[i]);
					String root = ServletActionContext.getServletContext().getRealPath("/upload");
					System.out.println("root:" + root);

					System.out.println("name:" + this.filesFileName[i]);
					System.out.println("type:" + this.filesContentType[i]);
					File destFile = new File(root, this.filesFileName[i]);
					
					OutputStream os = new FileOutputStream(destFile);
					byte[] b = new byte[1024 * 1024 * 10];
					int length = 0;
					while (true) {
						length = is.read(b);
						if (length < 0)
							break;
						os.write(b, 0, length);
					}
					is.close();
					os.close();
				}
				this.getResultMap().put("result", SUCCESS);
			}
		} catch (Exception e) {
			System.out.println("上传文件异常...");
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 获取上传文件进度条
	 * @return 进度条数据
	 * @throws Exception
	 */
	public String getUploadProgress() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		this.stateProgress = (StateProgress) session.getAttribute("stateProgress");
		if (stateProgress == null) {
			System.out.println("action is null");
			stateProgress = new StateProgress();
			stateProgress.setCurrentItem(0);
		} else {
			Double a = Double.parseDouble(stateProgress.getReadedBytes() + "");
			Double b = Double.parseDouble(stateProgress.getTotalBytes() + "");
			double result = a / b * 100;
			stateProgress.setRate((int) result);
			System.out.println("action:" + stateProgress.getRate());
		}
		this.getResultMap().put("result", SUCCESS);
		this.getResultMap().put("stateProgress", stateProgress);
		return SUCCESS;
	}
}
